package com.lifesting.front;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.MetaRule;
import com.sun.facelets.tag.Metadata;
import com.sun.facelets.tag.MetadataTarget;
import com.sun.facelets.tag.TagAttribute;

public class RnsRule extends MetaRule {
    final static class TextMetadata extends Metadata {

        private final String value;

        public TextMetadata(String value) {
            this.value = value;
        }

        public void applyMetadata(FaceletContext ctx, Object instance) {
            ((ITextAware)instance).setText(value);
        }
    }
    final static class TableForMetaData extends Metadata{

    	private final String value;
		public TableForMetaData(String value) {
			super();
			this.value = value;
		}
		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((ITableAware)instance).setTableFor(value);			
		}
    	
    }
	@Override
	public Metadata applyRule(String name, TagAttribute attribute,
			MetadataTarget meta) {
		if (meta.isTargetInstanceOf(Rns.class))
		{
			if ("value".equals(name))
			{
				return new TextMetadata(attribute.getValue());
			}
			else if ("tableFor".equals(name))
			{
				return new TableForMetaData(attribute.getValue());
			}
		}
		return null;
	}

}
